
$obj = function(i){return document.getElementById(i)};
function checkSubmit()
{
	if(document.form1.title.value==""){
		alert("标题不能为空！");
		document.form1.title.focus();
		return false;
	}

	if(document.form1.body.value==""){
		alert("请先填写内容！视频源添加请输入后按添加！");
		return false;
	}

	if(document.form1.typeid.value==0){
		alert("隶属栏目必须选择！");
		return false;
	}

	if ($obj("description").value.indexOf($obj("comefrom").value)==-1)
		$obj("description").value = $obj("comefrom").value+" "+$obj("description").value;
}
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
var lists = [];
function chkTips(id)
{
	vt_id=parseInt(id);
	if (vt_id==33 || vt_id==34)
	{
		$('#classtips').html("新番请依照 <font color=\"red\">【XX月/生肉/OVA等】新番名字 XX【字幕组】</font> 填写标题");
	}else if (vt_id == 40)
	{
		$('#classtips').html("<span style=\"color:red\">视频带有一定理论基础相关的自主研究，制作，开发，改造等原创视频。</span>");
	}else
	{
		document.getElementById('classtips').innerHTML="";
	}
}
function addNew()
{
	if ($("#editor_mode_basic").attr("checked") && $obj('odi_part').value=='')
	{
		alert('多P必须输入分P名称！');
		return false;
	}
	for (var i=0;i<lists.length;i++)
	{
		if ($obj('odi_source').value == lists[i].type && $obj('odi_link').value==lists[i].link)
		{
			alert('视频源重复！添加失败');
			return false;
		}
	}
	lists[lists.length] = {'type':$obj('odi_source').value,'link':$obj('odi_link').value,'part':$('#odi_part').val(),'desc':$('#odi_partdesc').val()};
	buildFromList();
	if ($obj('editor_mode_single').checked)
	{
		$('#btn_basic_add').attr("disabled",true);
	}
	$('#odi_partdesc').attr("disabled",false);
}
function list_moveUp(index)
{
	if (index == 0) return false;
	//if (lists.length<index) return false;
	var _tmp = lists[index];
	lists[index] = lists[index-1];
	lists[index-1] = _tmp;
	buildFromList()
}
function list_moveDown(index)
{
	if (lists.length<=index+1) return false;
	var _tmp = lists[index];
	lists[index] = lists[index+1];
	lists[index+1] = _tmp;
	buildFromList()
}
function buildFromList()
{
	//$obj('body').value="";
	vs="";
	for (var i=0;i<lists.length;i++)
	{
		var v_type = lists[i].type;
		switch (v_type)
		{
			case "youku": v_type = "优酷"; break;
			case "sina": v_type = "新浪"; break;
			case "qq": v_type = "腾讯"; break;
			case "6cn": v_type = "六间房"; break;
		}
		//$obj('body').value+='['+lists[i].type+']'+lists[i].link+'[/'+lists[i].type+']'+(lists[i].desc ? '[desc]'+lists[i].desc+'[/desc]' : "")+(lists[i].part ? '#p#'+lists[i].part+'#e#' : "")+"\n";
		if(v_type == "新浪"){
			$obj('body').value+="[mukio title='undefined' desc='undefined']vid="+lists[i].link+"[/mukio]";
		}else{
			$obj('body').value+="[mukio title='undefined' desc='undefined']vid="+lists[i].link+"&type="+lists[i].type+"[/mukio]";
		}
		vs+="<tr bgcolor=\"#ffffff\"><td>"+v_type+"</td><td>"+lists[i].link+"</td>"+($("#editor_mode_basic").attr("checked")?"<td>"+lists[i].part+"</td><td>"+lists[i].desc+"</td>":"")+"<td nowrap><a href=\"javascript:void()\" onclick=\"list_moveUp("+i+");\" title=\"上移\">↑</a> <a href=\"javascript:void()\" onclick=\"list_moveDown("+i+");\" title=\"下移\">↓</a> <a href=\"javascript:void()\" onclick='lists.remove("+i+"); $obj(\"btn_basic_add\").disabled=\"\"; buildFromList()' style='cursor:pointer;color:#ff6060'>删</a></td></tr>";
	}
	$obj('odi_list').innerHTML="<table cellpadding='1' cellspacing='1' bgcolor='#999999'><tr class=\"head\"><td nowrap>来源</td><td width='350'>视频ID</td>"+
		($("#editor_mode_basic").attr("checked") ? "<td nowrap=\"nowrap\">分页名称</td><td nowrap=\"nowrap\">分页简介</td>" : "")+"<td nowrap>操作</td></tr>"+vs+
		(lists.length==1 && $obj('editor_mode_single').checked ? '' : '<tr><td><select id="odi_source"><option value="sina">新浪</option><option value="youku">优酷</option><option value="qq">腾讯</option><option value="6cn">6间房</option></select></td>'+
		'<td id="odi_link_container"><input type="text" id="odi_link" style=\"width:340px\" size="60" value="" /></td>'+
		($("#editor_mode_basic").attr("checked") ? '<td><input type="text" id="odi_part" size="20" style="width:80px" value="" /></td><td><input type="text" id="odi_partdesc" size="40" style="width:220px" disabled="disabled" value="" /></td>' : "")+
		'<td><input type="button" value=" 添加 " id="btn_basic_add" onclick="addNew()" /></td></tr>')+
		"</table>";
	$("#odi_source").change(function(){
		if (this.value == "youku1")
		{
			$("#odi_link_container").html("<a href=\"http://otaku.im\" target=\"_blank\">优酷源投稿已禁止，可点这里使用第三方工具转投至新浪</a>");
		}else
		{
			$("#odi_link_container").html("<input type=\"text\" id=\"odi_link\" style=\"width:340px\" size=\"60\" value=\"\" />");
		}
	});
}
function select_Editor_Mode(mo)
{
	if (mo==0)
	{
		buildFromList();
		if (!$obj('editor_mode_single').checked)
		{
			$obj('btn_basic_add').disabled="";
		}
		$('#odi_list').fadeIn(500);
		$('#mode_advance').fadeOut(500);
	}else
	{
		//$obj("editor_mode_basic").disabled="disabled";
		$('#odi_list').fadeOut(500);
		$('#mode_advance').fadeIn(500);
	}
}

$obj('typeid').onclick=$obj('typeid').onchange=function()
{
	if (this.value==16){
		$obj("editor_mode_basic").checked = false;
		$obj("editor_mode_advance").checked = false;
		select_Editor_Mode(1);
	}
}
$("#odi_source").click(function(){
if (this.value=="flash")
{
	$("#flash_ratio").css("display","");
}else
{
	$("#flash_ratio").css("display","none");
}
});
$(function(){buildFromList()});

var suggest_func = {
		source: function( request, response ) {
			var tmp_term_lst = request.term.split(",");
			
			$.getJSON( "/suggest?jsoncallback=?", {
				term: tmp_term_lst[tmp_term_lst.length-1].replace(/　/g,""),
				rnd:Math.random()
			}, response );
		},
		search: function() {
			// custom minLength
			this.value=this.value.replace(/，/g,",");
			var term = this.value;
			var tmp_term_lst = term.split(",");
			if (tmp_term_lst[tmp_term_lst.length-1].length < 1) return false;
			
			if ( term.charCodeAt(0)<255 && term.length < 1 || term.length>40) {
				return false;
			}
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function( event, ui ) {
			var tmp_term_lst = this.value.split(",");
			_value = "";
			if (tmp_term_lst.length > 1)
			{
				for (i=0;i<tmp_term_lst.length-1;i++)
				{
					_value+=tmp_term_lst[i]+",";
				}
			}
			_value+=ui.item.value;
			this.value = _value;
			// $("#searchform").submit();
			return false;
		}
	};
var request_term = "";
if ($("#tags").length)
{
$("#tags").attr("autocomplete","off");
$("#tags").autocomplete(suggest_func)
		.data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a style=\"text-align:left\">" + item.value + "<em style=\"float:right;font-size:10px;\""+(item.match ? " title=\"(Match Token: "+item.match+")\"" : "")+">" + (item.desc ? item.desc : item.ref + "个")+"</em></a>" )
			.appendTo( ul );
		};
}
if ($("#sp_alias").length)
{
$("#sp_alias").attr("autocomplete","off");
$("#sp_alias").autocomplete(suggest_func)
		.data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a style=\"text-align:left\">" + item.value + "<em style=\"float:right;font-size:10px;\""+(item.match ? " title=\"(Match Token: "+item.match+")\"" : "")+">" + (item.desc ? item.desc : item.ref + "个")+"</em></a>" )
			.appendTo( ul );
		};
}

window.countStrlen = function(input_id, output_id, maxlen) {
	if (input_id == '' || output_id == '' || maxlen == '') {
		return false;
	}
	
	var count = $('#'+input_id).val().replace(/\r\n?/, "\n").length;
	
	if (maxlen < count) {
		$('#'+output_id).html('<span class="error">'+ count +'</span>');
	} else {
		$('#'+output_id).html(count);
	}
};